package com.jc.cloud.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jc.cloud.sign.bean.dto.FlowQuery;
import com.jc.cloud.sign.bean.entity.Contractlist;
import com.jc.cloud.sign.bean.entity.FlowApproverInfos;
import com.jc.cloud.sign.common.R;
import com.jc.cloud.sign.mapper.ContractlistMapper;
import com.jc.cloud.sign.mapper.FlowApproverInfosMapper;
import com.jc.cloud.sign.service.ContractlistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 合同管理 服务实现类
 * </p>
 *
 * @author cxc
 * @since 2023-11-16
 */
@Service
public class ContractlistServiceImpl extends ServiceImpl<ContractlistMapper, Contractlist> implements ContractlistService {

    @Autowired
    private ContractlistMapper contractlistMapper;

    @Autowired
    private FlowApproverInfosMapper flowApproverInfosMapper;

    @Override
    public R search(long page, long size, FlowQuery flowQuery) {
        /**
         *因为mapper.xml已经对查询条件进行筛选和拼接了
         */
        if (flowQuery==null){
            flowQuery=new FlowQuery();
        }
        Page<Contractlist> page1 = new Page<>(page, size);
        IPage data = contractlistMapper.searchPage(page1, flowQuery);
        return R.ok().put("data",data);

    }

    @Override
    public HashMap<Object, Object> selectBypage(Long current, Long limit, FlowQuery flowQuery) {
        //QueryWrapper,构建
        QueryWrapper<Contractlist> wrapper = new QueryWrapper<>();
        //多条件组合查询，动态sql
        Long flowApprovalStatus = flowQuery.getFlowApprovalStatus();
        String flowname = flowQuery.getFlowName();
        if (!StringUtils.isEmpty(flowname)) {
            wrapper.like("flowName", flowname);
        }
        if (flowApprovalStatus != 0) {
            wrapper.eq("flowApprovalStatus", flowApprovalStatus);
        }
        wrapper.orderByDesc("createOn");

        //创建page
        Page<Contractlist> pageCondition = new Page<>(current, limit);
//        调用方法，实现分页查询
        Page<Contractlist> page = contractlistMapper.selectPage(pageCondition, wrapper);
        for (Contractlist resultEntity : page.getRecords()) {
            FlowApproverInfos flowApproverInfosEntity = flowApproverInfosMapper.selectOne(
                    new QueryWrapper<FlowApproverInfos>()
                            .eq("flowId", resultEntity.getFlowId())
            );
            if (flowApproverInfosEntity != null) {
                resultEntity.setProxyOrganizationName(flowApproverInfosEntity.getProxyOrganizationName());
                resultEntity.setApproveName(flowApproverInfosEntity.getApproveName());
            }
        }
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();

        long total = pageCondition.getTotal();//总记录数
        List<Contractlist> records = pageCondition.getRecords();//数据list集合
        objectObjectHashMap.put("limit", limit);
        objectObjectHashMap.put("current", current);
        objectObjectHashMap.put("total", total);
        objectObjectHashMap.put("records", records);

        return objectObjectHashMap;
    }


//    @Autowired
//    private YourMapper yourMapper;
//
//    public Page<YourEntity> queryGoodsWithPage(int pageNum, int pageSize, String keyword) {
//        Page<YourEntity> page = new Page<>(pageNum, pageSize);
//        yourMapper.selectPage(page, null);  // 这里的 null 可以替换为自定义的查询条件
//        return page;
//    }
}
